<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/1/10
 * Time: 10:17
 * 添加管理员
 */
include_once 'admin_global.php';        //加载后台统一调用类文件
$db->Get_user_shell_check($uid, $shell);   //检测用户是否登录
$db->Get_user_type_check($uid, $shell);    //检测用户权限
//获取表单数据并写入数据库
if (isset($_POST['addAdmin'])) {
    $userName = trim($_POST['userName']);
    //检测该用户名是否已存在
    $sql = "SELECT * FROM admin WHERE userName='$userName'";
    $query = $db->query($sql);
    $num = $db->db_num_rows($query);
    if ($num == 0) {
        if (!empty($userName) && strlen($userName) <= 16) {
            $userPassword = sha1(trim($_POST['userPassword']));
            $userEmail = $_POST['userEmail'];
            $userType = $_POST['userType'];
            $userSex = $_POST['userSex'];
            if (!empty($_POST['userEnabled'])) {
                $userEnabled = 1;
            } else {
                $userEnabled = 0;
            }
            $userCreateTime = time();
            $userRemark = $_POST['userRemark'];
            $sql = "INSERT INTO admin(userID,userName,userPassword,userEmail,userType,userSex,userEnabled,userCreateTime,userRemark) VALUES(null,'$userName','$userPassword','$userEmail','$userType','$userSex',$userEnabled,'$userCreateTime','$userRemark') ";
            $query = $db->query($sql);
            if ($query == true) {
                echo "<script>alert('用户添加成功');window.location.href='listAdmin.php'</script>";
            } else {
                echo "<script>alert('用户添加失败');history.go(-1);</script>";
            }
        } else {
            echo "<script>alert('用户名长度超出范围！');history.go(-1);</script>";
        }
    } else {
        echo "<script>alert('该用户已存在！');history.go(-1);</script>";
    }
}
